#include "GLACRobot.h"
#include "DarkGDK.h"
GLACRobot::GLACRobot(void)
{
	memset(m_vPositions,0,sizeof(m_vPositions));
	memset(m_vBodyDirections,0,sizeof(m_vBodyDirections));
	memset(m_vHeadDirections,0,sizeof(m_vHeadDirections));

	// body
	int nID = GetGDKOBJID();
	m_nHeadID = nID;
 	dbMakeObjectBox(nID,15,20,6);
 	dbPositionObject(nID,0.0,0.0,-3);
 	dbColorObject (nID,dbRGB(255,0,0) );
	m_lstComponentID.push_back(nID);

	nID = GetGDKOBJID();
	m_nHeadID = nID;
	dbMakeObjectBox(nID,10,10,6);
	dbPositionObject(nID,0.0,0.0,-9);
	dbColorObject (nID,dbRGB(0,255,0) );
	m_lstComponentID.push_back(nID);

	nID = GetGDKOBJID();
	m_nFireBaseID = nID;
	dbMakeObjectBox(nID,1,10,1);
	dbPositionObject(nID,0.0,10.0,-9);
	dbColorObject (nID,dbRGB(0,255,0) );
	m_lstComponentID.push_back(nID);

}

GLACRobot::~GLACRobot(void)
{
	while(!m_lstComponentID.empty())
	{
		dbDeleteObject (m_lstComponentID.back());
		m_lstComponentID.pop_back();
	}
}

void GLACRobot::SetPosition( int nPositionX/*=0*/,int nPositionY/*=0*/,int nPositionZ/*=0*/ )
{
	int dx = nPositionX - m_vPositions[0];
	int dy = nPositionY - m_vPositions[1];
	int dz = nPositionZ - m_vPositions[2];
	m_vPositions[0] = nPositionX;
	m_vPositions[1] = nPositionY;
	m_vPositions[2] = nPositionZ;

	std::list<int>::iterator itr = m_lstComponentID.begin();
	while ( itr != m_lstComponentID.end() )
	{
		int nID = *itr;
		dbPositionObject(nID,dbObjectPositionX(nID)+dx,dbObjectPositionY(nID)+dy,dbObjectPositionZ(nID)+dz);
		itr++;
	}
	return;
}

int GLACRobot::TimeEscape( double fMillionSecond )
{

	return 0;
}

int GLACRobot::MoveAhead( double fStep )
{
	std::list<int>::iterator itr = m_lstComponentID.begin();
	while ( itr != m_lstComponentID.end() )
	{
		int nID = *itr;
		dbMoveObjectUp(nID,fStep);
		itr++;
	}
	return 0;
}

int GLACRobot::MoveBack( double fStep )
{
	MoveAhead(-fStep);
	return 0;
}

int GLACRobot::TurnLeft( double fStep )
{
	std::list<int>::iterator itr = m_lstComponentID.begin();
	while ( itr != m_lstComponentID.end() )
	{
		int nID = *itr;
		dbRollObjectLeft(nID,fStep);
		itr++;
	}
	return 0;
}

int GLACRobot::TurnRight( double fStep )
{
	TurnLeft(-fStep);
	return 0;
}